<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user) || isset($is_moderator))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  View Polls
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Get Polls
		//-----------------------------------------------------------------

		$polls_db = $db->getTable("SELECT * FROM poll ORDER BY id DESC");

		if($polls_db != FALSE)
		{
			foreach($polls_db as $idx=>$poll)
			{
				//-----------------------------------------------------------------
				//  Start from 1
				//-----------------------------------------------------------------

				$idx = $idx + 1;

				//-----------------------------------------------------------------
				//  Count All votes
				//-----------------------------------------------------------------

				$Total_poll_votes = $poll['votes1'] + $poll['votes2'] + $poll['votes3'] + $poll['votes4'] + $poll['votes5'] + $poll['votes6'] + $poll['votes7'] + $poll['votes8'] + $poll['votes9'] + $poll['votes10'];

				//-----------------------------------------------------------------
				//  Separate the options + Calculate the graph
				//-----------------------------------------------------------------

				for ($i = 1; $i <= 10; $i++)
				{
					if($Total_poll_votes != "0") $per_percent = round(($poll["votes$i"] * 100 / $Total_poll_votes), 1);
					else $per_percent = 0;

					if(isset($poll["option$i"])) $poll["option$i"] = "<tr class='con3'><td><table width='100%' cellspacing='0' cellpadding='1'><tr class='empty'><td>{$poll["option$i"]} [{$poll["votes$i"]} v.]</td><td width='20%' align='right'>$per_percent%</td></tr><tr class='con3'><td colspan='2'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='$per_percent%'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='2'><img src='templates/$template/images/progress/a.gif' width='2' height='16'></td><td width='100%' background='templates/$template/images/progress/b.gif'></td><td width='2'><img src='templates/$template/images/progress/c.gif' width='2' height='16'></td></tr></table></td><td width='100%'></td></tr></table></td></tr></table></td></tr>";
					else $poll["option$i"] = NULL;
				}
	
				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "
					<tr class='bottom'><td colspan='2'>{$language['Poll view2 desc']}$idx"; if($idx == "1") $layout .= " | {$language['Poll view2 desc2']} - <span class='ok'>{$language['Poll view2 desc3']}</span>"; else $layout .= " | {$language['Poll view2 desc2']} - <span class='normal'>{$language['Poll view2 desc4']}</span>"; $layout .= "</td></tr>
					<tr class='".row2color($idx)."'>
					<td width='40%' valign='top'>{$language['Poll view2 desc5']} {$poll['question']}<br><br>{$language['Poll view2 desc6']} $Total_poll_votes<br>{$language['Poll view2 desc7']} ".today_yesterday($poll['date'])."</td><td width='60%' valign='top'><table class='main' cellspacing='1' cellpadding='2'>{$poll['option1']}{$poll['option2']}{$poll['option3']}{$poll['option4']}{$poll['option5']}{$poll['option6']}{$poll['option7']}{$poll['option8']}{$poll['option9']}{$poll['option10']}</table></td>
					</tr>";
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "
				<tr class='con1'>
				<td colspan='3'>{$language['Poll view desc8']}</td>
				</tr>";
		}

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Poll view desc9']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/poll'><a href='$PHP_SELF?n=modules/poll'> &raquo; {$language['Poll view desc10']}</a></td></tr></table><br>" ?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Poll view2 title'] ?></td></tr>
			<?= $layout ?>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  Update Poll + View Edit Form
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Load the poll's row
		//-----------------------------------------------------------------

		$poll = $db->getLine("SELECT * FROM poll",$d);

		//-----------------------------------------------------------------
		//  Start the switch
		//-----------------------------------------------------------------

		switch($o)
		{
			//-----------------------------------------------------------------
			//  Edit Poll
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				if(!$question){ header("location: $PHP_SELF?n=modules/poll&a=2&d=$d&i=82"); exit; }
				if(!$option[1]){ header("location: $PHP_SELF?n=modules/poll&a=2&d=$d&i=83"); exit; }
				if(!$option[2]){ header("location: $PHP_SELF?n=modules/poll&a=2&d=$d&i=84"); exit; }

				//-----------------------------------------------------------------
				//  Take care of some things
				//-----------------------------------------------------------------

				$question = trim(jcm_stripslashes($question));
				for ($i = 1; $i <= 10; $i++) $option[$i] = trim(jcm_stripslashes($option[$i]));

				//-----------------------------------------------------------------
				//  Save the poll
				//-----------------------------------------------------------------

				$tmp['question'] = $question;

				for ($i = 1; $i <= 10; $i++)
				{
					$tmp["option$i"] = $option[$i];
					$tmp["votes$i"] = $vote[$i];
				}

				$db->updateRow("poll",$tmp,"id={$poll['id']}");

				//-----------------------------------------------------------------
				//  Write Log
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>poll</b> at the poll -> \''.$question.'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/poll&a=2&d=$d&i=80");

			break;

			default:

				//-----------------------------------------------------------------
				// Echo the back link
				//-----------------------------------------------------------------

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Poll edit desc7']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/poll'><a href='$PHP_SELF?n=modules/poll'> &raquo; {$language['Poll edit desc8']}</a></td></tr></table><br>" ?>
					<form method="post" action="<?= $PHP_SELF ?>?n=modules/poll">
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class="head"><td colspan="4"><?= $language['Poll edit title'] ?></td></tr>
					<tr class="bottom"><td colspan="3"><?= $language['Poll edit desc'] ?></td></tr>
					<tr class="con1"><td colspan="3"><input type="text" name="question" value="<?= $poll['question'] ?>" class="box" style="width:100%"></td></tr>
					<tr class="bottom"><td width="1%" align="center"><?= $language['Poll edit desc2'] ?></td><td width="90%"><?= $language['Poll edit desc3'] ?></td><td width="10%"><?= $language['Poll edit desc4'] ?></td></tr>
					<?
					for ($i = 1; $i <= 10; $i++)
						echo "<tr class='con1'><td align='center'>$i</td><td><input type='text' name='option[$i]' value='{$poll["option$i"]}' class='box' style='width:100%'></td><td><input type='text' name='vote[$i]' value='{$poll["votes$i"]}' class='box' style='width:100%'></td></tr>" ?>
					</table>
					<br>
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class='con1'>
					<td width="50%"><?= $language['Poll edit desc5'] ?></td>
					<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Poll edit desc6'] ?>' tabindex='3'></td>
					</tr>
					<input type="hidden" name="a" value="2">
					<input type="hidden" name="o" value="1">
					<input type="hidden" name="d" value="<?= $d ?>">
					</table></form>
				<?

			break;
		}

	break;

	//-----------------------------------------------------------------
	//  Delete Poll
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT question FROM poll WHERE id = '$d'");
		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>poll</b> the poll -> \''.$checker['question'].'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Delete the badword
		//-----------------------------------------------------------------

		$db->deleteRow("poll","id = '$d'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/poll&i=81");

	break;

	//-----------------------------------------------------------------
	//  Add new poll
	//-----------------------------------------------------------------

	case 4:

		//-----------------------------------------------------------------
		//  Begin Primary Check
		//-----------------------------------------------------------------

		if(!$question){ header("location: $PHP_SELF?n=modules/poll&i=82"); exit; }
		if(!$option[1]){ header("location: $PHP_SELF?n=modules/poll&i=83"); exit; }
		if(!$option[2]){ header("location: $PHP_SELF?n=modules/poll&i=84"); exit; }

		//-----------------------------------------------------------------
		//  Begin Secondary Check
		//-----------------------------------------------------------------

		for ($i = 1; $i <= 10; $i++)
		{
			if(!$option[$i]) $option[$i] = NULL;
			if(!$vote[$i]) $vote[$i] = NULL;
		}

		//-----------------------------------------------------------------
		//  Take care of some things
		//-----------------------------------------------------------------

		$question = trim(jcm_stripslashes($question));
		for ($i = 1; $i <= 10; $i++) $option[$i] = trim(jcm_stripslashes($option[$i]));

		//-----------------------------------------------------------------
		//  Save the poll
		//-----------------------------------------------------------------

		$tmp['question'] = $question;
		$tmp['date'] = time();

		for ($i = 1; $i <= 10; $i++)
		{
			$tmp["option$i"] = $option[$i];
			$tmp["votes$i"] = $vote[$i];
		}

		$db->insertRow("poll",$tmp);

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc3'].' <b>poll</b> the poll -> \''.$question.'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/poll&i=79");

	break;

	//-----------------------------------------------------------------
	//  View Poll Form
	//-----------------------------------------------------------------

	default:

		$polls_db = $db->getTable("SELECT id, question FROM poll ORDER BY id DESC LIMIT 10");

		if($polls_db != FALSE)
		{
			foreach($polls_db as $idx=>$poll)
			{
				//-----------------------------------------------------------------
				//  Start from 1
				//-----------------------------------------------------------------

				$idx++;

				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."' onclick=window.location.href='$PHP_SELF?n=modules/poll&a=1'>
					<td width='1%'>$idx</td>
					<td width='91%'>{$poll['question']}"; if($idx == "1") $layout .= " | {$language['Poll view desc3']} - <span class='ok'>{$language['Poll view desc4']}</span>"; else $layout .= " | {$language['Poll view desc3']} - <span class='normal'>{$language['Poll view desc5']}</span>"; $layout .= "</td>
					<td width='8%'><a href='$PHP_SELF?n=modules/poll&a=2&d={$poll['id']}'><img src='templates/$template/images/buttons/edit.gif' border='0' alt='{$language['Poll view desc6']}'></a><a href='$PHP_SELF?n=modules/poll&a=3&d={$poll['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Poll view desc7']}'></a></td>
					</tr>";
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "<tr class='con1'><td colspan='3'>{$language['Poll view desc8']}</td></tr>";
		}

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Poll view title'] ?></td></tr>
			<tr class="bottom"><td><?= $language['Poll view desc'] ?></td><td colspan="2"><?= $language['Poll view desc2'] ?></td></tr>
			<?= $layout ?>
			</table>
			<br>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/poll">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Poll add title'] ?></td></tr>
			<tr class="bottom"><td colspan="3"><?= $language['Poll add desc'] ?></td></tr>
			<tr class="con1"><td colspan="3"><input type="text" name="question" class="box" style="width:100%"></td></tr>
			<tr class="bottom"><td width="1%" align="center"><?= $language['Poll add desc2'] ?></td><td width="90%"><?= $language['Poll add desc3'] ?></td><td width="10%"><?= $language['Poll add desc4'] ?></td></tr>
			<? for ($i = 1; $i <= 10; $i++) echo "<tr class='con1'><td align='center'>$i</td><td><input type='text' name='option[$i]' class='box' style='width:100%'></td><td><input type='text' name='vote[$i]' class='box' style='width:100%'></td></tr>"; ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='con1'>
			<td width="50%"><?= $language['Poll add desc5'] ?></td>
			<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Poll add desc6'] ?>' tabindex='3'></td>
			</tr>
			<input type="hidden" name="a" value="4">
			</table></form>
		<?
}
?>